package com.eva.shiro.service;

import com.eva.dao.system.SystemPermissionMapper;
import com.eva.dao.system.SystemRoleMapper;
import com.eva.dao.system.model.SystemDepartment;
import com.eva.dao.system.model.SystemPermission;
import com.eva.dao.system.model.SystemRole;
import com.eva.framework.common.constants.ResponseStatus;
import com.eva.framework.common.exception.BusinessException;
import com.eva.framework.config.AppConfig;
import com.eva.framework.rbac.model.RbacUserInfo;
import com.eva.service.system.SystemDepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 登录服务
 */
@Slf4j
@Service
public class LoginService {

    @Resource
    private AppConfig appConfig;

    @Resource
    private SystemRoleMapper systemRoleMapper;

    @Resource
    private SystemPermissionMapper systemPermissionMapper;

    @Resource
    private SystemDepartmentService systemDepartmentService;

    /**
     * 退出登录
     */
    public void logout () {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
    }

    /**
     * 补充登录用户信息
     *
     * @param loginUserInfo 登录信息
     */
    public void padding(RbacUserInfo loginUserInfo) {
        List<SystemRole> roles = systemRoleMapper.selectByUserId(loginUserInfo.getId());
        // 设置角色
        Set<Integer> roleIds = new HashSet<>();
        Set<String> roleCodes = new HashSet<>();
        for (SystemRole role : roles) {
            roleIds.add(role.getId());
            roleCodes.add(role.getCode());
        }
        loginUserInfo.setRoles(roleCodes);
        // 设置权限
        List<SystemPermission> permissions = systemPermissionMapper.selectByUserId(loginUserInfo.getId());
        Set<String> permissionCodes = new HashSet<>();
        for (SystemPermission permission : permissions) {
            permissionCodes.add(permission.getCode());
        }
        loginUserInfo.setPermissions(permissionCodes);
        loginUserInfo.setIsSuperAdmin(roleCodes.contains(appConfig.getSuperAdminRole()));
        // 设置菜单权限
        loginUserInfo.setMenuPermissionIds(Collections.emptySet());
        if (!roleIds.isEmpty()) {
            loginUserInfo.setMenuPermissionIds(systemPermissionMapper.selectMenuPermissionIdsByRoleIds(roleIds));
        }
        // 设置功能权限
        loginUserInfo.setMenuFuncPermissionIds(Collections.emptySet());
        if (!roleIds.isEmpty()) {
            loginUserInfo.setMenuFuncPermissionIds(systemPermissionMapper.selectMenuFuncPermissionIdsByRoleIds(roleIds));
        }
        // 设置系统配置权限
        loginUserInfo.setSystemConfigPermissionIds(Collections.emptySet());
        if (!roleIds.isEmpty()) {
            loginUserInfo.setSystemConfigPermissionIds(systemPermissionMapper.selectSystemConfigPermissionIdsByRoleIds(roleIds));
        }
        // 设置部门
        List<SystemDepartment> departments = systemDepartmentService.findByUserId(loginUserInfo.getId());
        if (departments.isEmpty()) {
            throw new BusinessException(ResponseStatus.NOT_ALLOWED, "当前账号没有部门信息，请联系管理员进行配置！");
        }
        loginUserInfo.setDepartmentId(departments.get(0).getId());
        // 设置是否为部门负责人
        loginUserInfo.setIsDepartmentLeader(systemDepartmentService.isLeader(loginUserInfo.getId()));
    }
}
